<?php

namespace App\Http\Controllers\YApi;

// use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;
use Illuminate\Pagination\LengthAwarePaginator;

use App\Models\Book;
use App\Models\UsersReg;
use App\Models\Shuzhai; // 书摘

class ShuzhaiController extends Controller
{

    public function bookshuzhailist($bookid)
    {
        // 对应的书籍的书摘列表
        $bookshuzhailists = Shuzhai::where('bookid', $bookid)->orderBy('add_time', 'desc')->paginate(15);

        foreach ($bookshuzhailists as $bookshuzhailist) {
            //得到发布长评论的会员资料
            $bookshuzhailist['users'] = UsersReg::find($bookshuzhailist->uid);
        }
        
        return $bookshuzhailists;     
    }

    public function shuzhailist()
    {
        // 全部的书摘列表
        $shuzhailists = Shuzhai::orderBy('add_time', 'desc')->paginate(10);
        foreach($shuzhailists as $shuzhailist)
        {
            // 对应的会员信息
            $shuzhailist['users'] = UsersReg::find($shuzhailist->uid);
        }
        foreach($shuzhailists as $shuzhailist)
        {
            // 对应的书籍信息
            $shuzhailist['books'] = Book::find($shuzhailist->bookid,['book_id', 'book_name', 'zuozhe']);
        }
        return $shuzhailists;
    }

    public function saveshuzhai()
    {
        // 保存书摘内容
        $shuzhai = new Shuzhai;
        $shuzhai->uid = request::input('uid');
        $shuzhai->bookid = request::input('bookid');
        $shuzhai->content = request::input('shuzhaineirong');
        $shuzhai->xiangfa = request::input('shuzhaixiangfa');
        $shuzhai->device = request::input('device');
        $shuzhai->add_time = time();
        $shuzhai->save();

        return 1;
    }
    
}